package com.auth.domain.service.impl;

import com.auth.common.util.IPageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.auth.control.model.AuthDto;
import com.auth.domain.mapper.authmanage.AuthMapper;
import com.auth.domain.module.po.authmanage.Auth;
import com.auth.domain.module.vo.authmanage.AuthVO;
import com.auth.domain.service.IAuthService;
import com.bean.ObjectMapperUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author cyj
 * @since 2020-10-20
 */
@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, Auth> implements IAuthService {

    @Override
    public Auth getAuthByAuth(String auth) {
        Auth condition = new Auth().setAuth(auth);
        return this.baseMapper.selectOne(new QueryWrapper<Auth>(condition));
    }

    @Override
    public IPage<AuthVO> getAuths(Page page) {
        IPage<Auth> result = this.page(page);
        return IPageUtil.copy(result, AuthVO.class);
    }

    @Override
    public AuthVO saveAuth(AuthDto authDto) {
        Auth auth = ObjectMapperUtils.clone(authDto, Auth.class);
        this.save(auth);
        return ObjectMapperUtils.clone(auth, AuthVO.class);
    }

    @Override
    public AuthVO updateAuth(AuthDto authDto) {
        Auth auth = ObjectMapperUtils.clone(authDto, Auth.class);
        this.updateById(auth);
        return this.selectAuth(authDto.getId());
    }

    @Override
    public AuthVO deleteAuth(Long id) {
        AuthVO authVO = this.selectAuth(id);
        this.removeById(id);
        return authVO;
    }

    @Override
    public AuthVO selectAuth(Long id) {
        Auth auth = this.getById(id);
        return ObjectMapperUtils.clone(auth, AuthVO.class);
    }

    @Override
    public List<AuthVO> getByIds(Set<Long> authIds) {
        if (CollectionUtils.isEmpty(authIds)) {
            return Collections.EMPTY_LIST;
        }
        LambdaQueryWrapper<Auth> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.in(Auth::getId, authIds);
        List<Auth> list = this.list(lambdaQueryWrapper);
        return ObjectMapperUtils.clones(list, AuthVO.class);
    }
}
